package com.project.springboot.controller;

import com.gitee.yongzhuzl.commonutil.util.empty.EmptyUtil;
import com.gitee.yongzhuzl.commonutil.util.excel.PoiExcelUtil;
import com.gitee.yongzhuzl.commonutil.util.file.FileUploadUtil;
import com.gitee.yongzhuzl.commonutil.util.file.FileUtil;
import com.gitee.yongzhuzl.commonutil.util.result.ResponseResult;
import com.project.springboot.service.file.FileService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * description: 文件相关API
 * @author 朱正磊
 * @date 2023-12-18 10:11:29
 */
@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private FileService fileService;

    /**
     * description: 下载文件到服务器
     * @author 朱正磊
     * @date 2023-12-18 10:14:24
     * @return 无业务返回值
     */
    @GetMapping("/downloadFileToServer")
    public ResponseResult<Void> downloadFileToServer() {
        String fileUrl= "https://t7.baidu.com/it/u=963301259,1982396977&fm=193&f=GIF";
        String filePath = "F:\\zzl\\workfile\\图片\\其他\\";
        String extension = ".gif";
        fileService.downloadFileToServer(fileUrl, filePath, extension);
        return ResponseResult.buildSuccess();
    }

    /**
     * description: 下载文件到客户端
     * @author 朱正磊
     * @date 2023-12-18 10:14:24
     * @return 返回HTTP响应对象
     */
    @GetMapping("/downloadFileToClient")
    public ResponseEntity<FileSystemResource> downloadFileToClient() {
        String filePath = "F:\\zzl\\workfile\\图片\\卡通\\katong.jpeg";
        return fileService.downloadFileToClient(new File(filePath));
    }

    /**
     * description: 下载zip压缩文件到客户端
     * @author 朱正磊
     * @date 2023-12-18 10:14:24
     * @return 返回HTTP响应对象
     */
    @GetMapping("/downloadZipToClient")
    public ResponseEntity<FileSystemResource> downloadZipToClient() {
        String zipPathDir = "F:\\zzl\\workfile\\图片\\卡通\\";
        String zipFileName = "test.zip";
        List<String> fileList = Arrays.asList("F:\\zzl\\workfile\\图片\\卡通\\katong.jpeg",
                "F:\\zzl\\workfile\\图片\\其他\\fengjing.gif", "F:\\zzl\\workfile\\图片\\药品\\黄芩.jpeg");
        return fileService.downloadZipToClient(zipPathDir, zipFileName, fileList);
    }

    /**
     * description: 上传文件1
     * @author 朱正磊
     * @date 2025-09-01 08:55:20
     * @param file 文件
     * @return 返回自定义响应对象
     */
    @PostMapping("/uploadFile")
    public ResponseResult<Void> uploadFile(MultipartFile file) {
        try {
            // 其中最后一个\不能省略
            String fileFullPath = FileUploadUtil.uploadFile(file, "D:\\img\\test\\");
            log.info("上传文件全路径：{}", fileFullPath);
        } catch (Exception e) {
            return ResponseResult.buildError(e.getMessage());
        }
        return ResponseResult.buildSuccess();
    }

    /**
     * description: 上传文件2
     * @author 朱正磊
     * @date 2025-09-01 08:55:45
     * @param file 文件
     * @return 返回自定义响应对象
     */
    @PostMapping("/upFile")
    public ResponseResult<Void> upFile(MultipartFile file) {
        try {
            // 文件夹路径最后面有没有文件分隔符都可以
            String targetPath = FileUploadUtil.upFile(file, "D:/img/test");
            log.info("上传后的文件完整路径：{}", targetPath);

            // 结果比如：/F:/zzl/project/myself/springboot_demo_jdk21，最后面没有文件分隔符
            String serverPath = FileUploadUtil.getServerPath();
            log.info("获取tomcat服务器webapps所在的绝对路径：{}", serverPath);
        } catch (IOException e) {
            return ResponseResult.buildError(e.getMessage());
        }
        return ResponseResult.buildSuccess();
    }

    /**
     * description: 测试MultipartFile转File
     * @author 朱正磊
     * @date 2025-09-01 10:02:29
     * @param mFile 文件
     * @return 返回自定义响应对象
     */
    @PostMapping("/importExcel1")
    public ResponseResult<Void> importExcel1(@RequestParam("file") MultipartFile mFile) {
        File file = null;
        try {
            // MultipartFile转File
            file = FileUtil.multipartFileToFile(mFile);
            // 读取Excel文件数据
            List<List<String>> list = PoiExcelUtil.readExcel(file, "药架信息", null);
            log.info("读取Excel文件数据1：{}", list);
        } catch (Exception e) {
            return ResponseResult.buildError(e.getMessage());
        } finally {
            if (EmptyUtil.isNotEmpty(file)) {
                FileUtil.deleteTempFile(file);
                log.info("成功删除本地临时文件！");
            }
        }
        return ResponseResult.buildSuccess();
    }

    /**
     * description: 测试MultipartFile转File
     * @author 朱正磊
     * @date 2025-09-01 10:02:36
     * @param mFile 文件
     * @return 返回自定义响应对象
     */
    @PostMapping("/importExcel2")
    public ResponseResult<Void> importExcel2(@RequestParam("file") MultipartFile mFile) {
        File file = null;
        try {
            // MultipartFile转File
            file = FileUtil.mFileToFile(mFile);
            // 读取Excel文件数据
            List<List<String>> list = PoiExcelUtil.readExcel(file, "药架信息", null);
            log.info("读取Excel文件数据2：{}", list);
        } catch (Exception e) {
            return ResponseResult.buildError(e.getMessage());
        } finally {
            if (EmptyUtil.isNotEmpty(file)) {
                FileUtil.delete(file);
                log.info("本地临时文件成功删除！");
            }
        }
        return ResponseResult.buildSuccess();
    }

}
